#if !defined MOBJECT_H
#define MOBJECT_H

class DllExport CMObject
{
public:
	CMObject(_variant_t& tvar);	
	CMObject()
	{ p = NULL;	}
	CMObject(IDispatchPtr lp)
	{
		p=lp; 
		//if ( (p=lp) != NULL) p->AddRef();
	}
	CMObject(IUnknown* lp)
	{
		p=NULL;
		if (lp != NULL)	lp->QueryInterface(IID_IDispatch, (void **)&p);
	}
	~CMObject()
	{ 
		/*if (p)
			p->Release();*/
		p=NULL;
	}
	void Release() 
	{
		/*try
		{
			if (p)	p->Release();
		}
		catch(...){}*/
		p=NULL;
	}

	HRESULT CreateObject(const CLSID clsid);
	HRESULT CreateObject(LPCSTR objName);
	HRESULT GetActiveObject(const CLSID clsid);
	HRESULT GetActiveObject(LPCSTR objName);

	operator IDispatchPtr() {return p;}
	IDispatch& operator*() { return *p; }
	IDispatchPtr operator->() {return p; }
	IDispatchPtr operator=(IDispatchPtr lp);
	IDispatchPtr operator=(IUnknown* lp);
	IDispatchPtr operator=(_variant_t& newVar);
	BOOL operator!(){return (p == NULL) ? TRUE : FALSE;}

	_variant_t GetPropertyByName(LPCSTR lpsz);
	_variant_t GetProperty(DISPID dwDispID);
	HRESULT PutPropertyByName(LPCSTR lpsz, VARIANT* pVar);
	HRESULT PutProperty(DISPID dwDispID, VARIANT* pVar);
    static HRESULT GetProperty(IDispatch* pDisp, DISPID dwDispID,VARIANT* pVar);
	static HRESULT PutProperty(IDispatch* pDisp, DISPID dwDispID,VARIANT* pVar);
	HRESULT GetIDOfName(LPCSTR lpsz, DISPID* pdispid);
	_variant_t Invoke0(DISPID dispid);
	_variant_t Invoke0(LPCSTR lpszName);
	_variant_t Invoke(DISPID dispid, long nParamCount, VARIANT* ...);
	_variant_t Invoke(LPCSTR sName,long nParamCount,VARIANT* ...);

public://Attributes
	HRESULT GetWindow(HWND* Hwnd);
	IDispatchPtr p;

protected:
	_variant_t InvokeV(DISPID dispid,long nParamCount,va_list argList);
};
#endif